package com.naihe.anderson.sms.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.entity.Note;

public class NoteAdapter extends BaseAdapter {

	Context mContext;
    List<Note> objects;
    Date nowDate;

    public NoteAdapter(Context context, List<Note> objects) {
        mContext = context;
        this.objects = objects;
        nowDate = new Date();
    }
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return objects.size();
	}

	@Override
	public Note getItem(int position) {
		// TODO Auto-generated method stub
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.simple_list_note_item, null);
        } else {
            view = convertView;
        }
        TextView noteBody = (TextView) view.findViewById(R.id.text1);
        ImageView lock = (ImageView) view.findViewById(R.id.lock);
        ImageView pictype = (ImageView) view.findViewById(R.id.pictype);
        TextView time = (TextView) view.findViewById(R.id.time);
        Note note = getItem(position);
        noteBody.setText(note.title);
        if(TextUtils.isEmpty(note.cipher)){
        	lock.setVisibility(View.INVISIBLE);
        } else {
        	lock.setVisibility(View.VISIBLE);
        }
        if(TextUtils.isEmpty(note.picpath)){
        	pictype.setVisibility(View.INVISIBLE);
        } else {
        	pictype.setVisibility(View.VISIBLE);
        }
        time.setText(getDataFormat(note.createTime));
        
        note = null;
		return view;
	}
	
	private String getDataFormat(long time){
        String formatTime = "";
        Date date = new Date(time);
        try {
            if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==nowDate.getDate()){
				formatTime = mContext.getResources().getString(R.string.today) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==(nowDate.getDate()-1)){
				formatTime = mContext.getResources().getString(R.string.yesterday) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear()){
				formatTime = new SimpleDateFormat("MM-dd").format(date);
			}else{
				formatTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
			}
        } catch (Exception e) {
            formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);;
        }
        return formatTime;
    }
	
}
